package view;

import Dao.RedisDao;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout.Alignment;
import java.awt.Font;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LoginGUI extends JFrame {

    private JPanel contentPane;
    private JTextField userNameText;
    private JPasswordField passwordText;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LoginGUI frame = new LoginGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public LoginGUI() {
        setTitle("\u7BA1\u7406\u7CFB\u7EDF\u767B\u5F55");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 350, 350);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JLabel lblNewLabel = new JLabel("\u6E38\u6CF3\u9986\u4F1A\u5458\u7BA1\u7406\u7CFB\u7EDF");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 25));

        JLabel lblNewLabel_1 = new JLabel("\u7528\u6237\u540D");

        JLabel lblNewLabel_2 = new JLabel("\u5BC6  \u7801");

        userNameText = new JTextField();
        userNameText.setColumns(10);

        passwordText = new JPasswordField();
        passwordText.setColumns(10);

        JButton btnNewButton = new JButton("\u767B\u5F55");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                LoginActionPerformed(e);
            }
        });
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(66)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addComponent(lblNewLabel)
                                                .addContainerGap())
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                                .addComponent(lblNewLabel_1)
                                                                .addGap(18)
                                                                .addComponent(userNameText, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                                .addComponent(lblNewLabel_2)
                                                                .addGap(18)
                                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(passwordText, GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                                                                        .addComponent(btnNewButton))))
                                                .addGap(95))))
        );
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(32)
                                .addComponent(lblNewLabel)
                                .addGap(33)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(userNameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(30)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_2)
                                        .addComponent(passwordText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(59)
                                .addComponent(btnNewButton)
                                .addContainerGap(53, Short.MAX_VALUE))
        );
        contentPane.setLayout(gl_contentPane);
    }
    //对登录事件处理
    protected void LoginActionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String name = this.userNameText.getText();
        String pwd = new String(this.passwordText.getText());
        if (name.isEmpty()){
            JOptionPane.showMessageDialog(null,"用户名不能为空");
            return;
        }
        if (pwd.isEmpty()){
            JOptionPane.showMessageDialog(null,"密码不能为空");
            return;
        }
        boolean b = RedisDao.contaisJedis(name,pwd);
        if(b){
            this.dispose();
            new MainGUI(name).setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null,"用户名或密码错误");
            this.passwordText.setText("");
            this.userNameText.setText("");
        }
    }
}
